package com.vieewer.btdemo.utils;

import com.lindroid.androidutilskt.extension.shortToast
import com.vieewer.classicbt.CLog
import com.vieewer.classicbt.Connect
import com.vieewer.classicbt.listener.TransferProgressListener
import java.nio.ByteBuffer

class CommunicateToPhoneHelper private constructor() {

    var connect: Connect? = null


    private object Instance {
        public val sInstance = CommunicateToPhoneHelper()
    }

    companion object {

        val instance: CommunicateToPhoneHelper
            get() = Instance.sInstance
    }

    public fun write(text : String) {
        val bytes = text.toByteArray()

        val b = ByteBuffer.allocate(bytes.size)
        b.put(bytes)

        LogUtil.e("yjj", "CommunicateToPhoneHelper" + b.array().toString())

        connect?.write(b.array(), object : TransferProgressListener {
//            override fun disconnected() {
//                t("bluetooth has disconnected")
//
//            }

            override fun transferSuccess(bytes: ByteArray?) {
                shortToast("send message successful")

            }

            override fun transferFailed(msg: Exception) {
                msg.message?.run {
                    shortToast(this)
                }

            }

            override fun transfering(progress: Int) {
                CLog.e("write progress:$progress")
            }
        })
    }






}
